<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/28
 * Time: 19:07
 */

namespace app\MAPP\shop\mode\lists;


use core\lib\BaseControlerDB;
use core\lib\Config;
use function core\p;

class func extends BaseControlerDB
{
    var $conf;
    function __USERFUNC()
    {
        $this->conf = Config::all("SQLlib\\shoping\\sql");
    }

    public function lists($var){
        $var = $var ?? $var == 0 ||
            trim($var) == "" ||
            !is_numeric($var)?$var:0;

        $temp = $this->db->fetchall(preg_replace("/\?/",$var,$this->conf["lists"]));
        return $temp;
    }

    function car($id){
            if (isset($_COOKIE[$id])){
                p($_COOKIE[$id]);
                setcookie($id,$_COOKIE[$id]+1,time()+3600*10,"/");
            }else {
                setcookie($id, 1, time() + 3600 * 10, "/");
            }
    }

    function deccar($id){
        if (isset($_COOKIE[$id])){
            p($_COOKIE[$id]);
            $vars = ($_COOKIE[$id]-1)<0 ? $_COOKIE[$id] = 0:$_COOKIE[$id]-1;
            setcookie($id,$vars,time()+3600*10,"/");
        }else {
            setcookie($id, 0, time() + 3600 * 10, "/");
        }
    }

    function delocar($id){
        if (isset($_COOKIE[$id])){
            setcookie($id,"",time()-1,"/");
        }
    }

    function delcar(){
        foreach ($_COOKIE as $k => $v){
            if (is_numeric($v)){
                setcookie($k,"",time()-1,"/");
            }
        }
    }

    function mcar(){
        $res = [];
        $sum = 0;
        foreach ($_COOKIE as $k => $v){
            if (is_numeric($k)){
                $temp = [];
                $temp2 = $this->db->pre_exec($this->conf["get_info"],[$k]);
                $temp["id"] = $k;
                $temp["name"] = $temp2[0]["name"];
                $temp["num"] = $v;
                $temp["price"] = $temp2[0]["price"];
                $temp["img"] = $temp2[0]["img"];
                $temp["costs"] = $temp2[0]["price"] * $v;
                $sum+= $temp["costs"];
                $res[] = $temp;
            }
        }
        return [$sum,$res];
    }

    function savecar(){
        foreach ($_COOKIE as $k => $v){
            if (is_numeric($k)){
                $v = is_null($v) || $v == ""?$v:0;
                $this->db->pre_exec($this->conf["SAVECAR"],[1,$k,$v]);
            }
        }
    }
}